package com.zc.basic.infra.advice;

import com.zc.commons.exception.MsgHandlerException;
import com.zc.commons.pojo.ResponseData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice(basePackages = {"com.zc.basic.app"})
public class ResponseExceptionHandler {

    @ExceptionHandler({MsgHandlerException.class})
    @ResponseBody
    public ResponseData handleMsgException(MsgHandlerException e) {
        return ResponseData.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResponseData<Object> handleResponseException(Exception e) {

        String msg = "服务器内部错误！" + " \n " + e.getMessage();
        return ResponseData.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }
}
